import { createSlice, createAsyncThunk } from "@reduxjs/toolkit";
import { userInfo, getConfig, getInvitedCode } from "@/api/module/user";
import {
  announceType,
  describeValType,
  productRewardType,
} from "@/common/model/response";

// 如果localStorage里没有主题配置，则使用globalConfig里的初始化配置
interface userStore {
  userInfo: Awaited<ReturnType<typeof userInfo>>["data"];
  invitedCode: string;
  config: Awaited<ReturnType<typeof getConfig>>["data"];
  productReward: productRewardType;
  describle: describeValType;
  newAnnounce: announceType;
  rules: string;
}
//该store分库的初始值
const initialState: userStore = {
  userInfo: {
    user_id: 0,
    address: "",
    level_name: "",
    paypwd_status: 0,
    USDT: {
      account_name: "",
      account_code: "",
      balance: 0,
    },
    XJB: {
      account_name: "",
      account_code: "",
      balance: 0,
    },
    ZCB: {
      account_name: "",
      account_code: "",
      balance: 0,
    },
    JHB: {
      account_name: "",
      account_code: "",
      balance: 0,
    },
    FTB: {
      account_name: "",
      account_code: "",
      balance: 0,
    },
    STF: {
      account_name: "",
      account_code: "",
      balance: 0,
    },
    GBJ: {
      account_name: "",
      account_code: "",
      balance: 0,
    },
    GPB: {
      account_name: "",
      account_code: "",
      balance: 0,
    },
    total_value: 0,
    profit_yesterday: 0,
    c2c_data: {
      amount_today: 0,
      price: 0,
      last_matched_time: "",
    },
  },
  invitedCode: "",
  config: {
    recharge: {
      min_rechange_usdt: "",
      max_rechange_usdt: "",
    },
    withdrawal: {
      fee_usdt: "",
      min_withdrawal_usdt: "",
    },
  },
  productReward: {
    1: [],
    2: [],
    3: [],
    4: [],
    5: [],
    6: [],
    7: [],
    8: [],
    9: [],
  },
  describle: {
    1: "",
    2: "",
    3: "",
    4: "",
    5: "",
    6: "",
    7: "",
    8: "",
    9: "",
  },
  newAnnounce: {
    id: 0,
    title: "",
    cover: "",
    addtime: "",
    content: "",
  },
  rules: "",
};

export const fetchUserInfo = createAsyncThunk(
  "fetchUserInfo",
  (_, { dispatch }) => {
    userInfo().then((res) => {
      if (res.code == 0) {
        // res.data.ZCB.balance = 1212;
        dispatch(setUserInfo(res.data));
      }
    });
  }
);
export const fetchInviteCode = createAsyncThunk(
  "fetchUserInfo",
  (_, { dispatch }) => {
    getInvitedCode().then((res) => {
      if (res.code == 0) {
        dispatch(setInvitedCode(res.data));
      }
    });
  }
);
export const fetchConfig = createAsyncThunk(
  "fetchUserInfo",
  (_, { dispatch }) => {
    getConfig().then((res) => {
      if (res.code == 0) {
        dispatch(setConfig(res.data));
      }
    });
  }
);
export const fetchAnnounce = createAsyncThunk("fetchUserInfo", (_, {}) => {
  // getNewAnnounce().then((res) => {
  //   if (res.code == 0) {
  //     dispatch(setAnnounce(res.data));
  //   }
  // });
});
export const fetchRules = createAsyncThunk("fetchUserInfo", (_, {}) => {
  // getNewAnnounce().then((res) => {
  //   if (res.code == 0) {
  //     dispatch(setAnnounce(res.data));
  //   }
  // });
});

export const userSlice = createSlice({
  // store分库名称
  name: "userInfo",
  // store分库初始值
  initialState,
  reducers: {
    setUserInfo: (state, action) => {
      state.userInfo = action.payload;
    },
    setInvitedCode: (
      state,
      action: {
        payload: Awaited<ReturnType<typeof getInvitedCode>>["data"];
        type: string;
      }
    ) => {
      state.invitedCode = action.payload.invite_url;
      // state.productReward = action.payload.product_reward;
      // state.describle = action.payload.describe_val;
    },
    setConfig: (state, action) => {
      state.config = action.payload;
    },
    setAnnounce: (state, action) => {
      state.newAnnounce = action.payload;
    },
    setRules: (state, action) => {
      state.rules = action.payload;
    },
  },
});

export const { setUserInfo, setInvitedCode, setAnnounce, setConfig, setRules } =
  userSlice.actions;
export default userSlice.reducer;
